package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeSectionInfoService;
import com.bidevalution.business.service.BeSectionParticipantService;


import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionParticipant;
import com.bidevalution.dao.mapper.BeSectionParticipantMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.qtp.core.common.RemoteService;
import com.qtp.core.model.Paging;
import com.qtp.core.util.GsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投标单位信息业务实现层
 *
 * @author shenh@glodon.com
 * @date 2019-04-29 10:42
 */
@Service
public class BeSectionParticipantServiceImpl implements BeSectionParticipantService {

    @Value("${ids.url}")
    private String ptIdsUrl;

    @Autowired
    private BeSectionParticipantMapper beSectionParticipantMapper;

    @Autowired
    private BeSectionInfoService beSectionInfoService;

    /**
     * 获取总的投标单位参与家数
     *
     * @param ptSectionId
     * @return
     */
    @Override
    public Long queryParticipantCount(String ptSectionId) throws Exception {
        if (StringUtils.isBlank(ptIdsUrl)) {
            throw new Exception("未配置平台ids地址");
        }
        Map<String, Object> params = Maps.newHashMap();
        params.put("bsID", ptSectionId);
        params.put("busiType", "1");
        String bidViewUrl = ptIdsUrl + "/bidViewWz.shtml";
        //调用接口
        String response = RemoteService.getRemoteInfo(bidViewUrl, params);
        if (StringUtils.isNotBlank(response)) {
            Map<String, Object> resultMap = GsonUtil.jsonToMapWithGson(response);
            if (resultMap != null) {
            	
                List<Map<String, String>> list = (List<Map<String, String>>) resultMap.get("bids");
                if (list != null) {
                	List<Map<String, String>> tempList = new ArrayList<Map<String,String>>();
                    //daizf 修改 不能遍历list时候删除list的值
                	for (Map<String, String> map : list) {
                        if (map == null || map.get("fileID") == null) {
                            
                            tempList.add(map);
                        }
                    }
                    for(Map<String, String> map : tempList) {
                    	list.remove(map);
                    }
                    return Long.valueOf(list.size());
                }
            }
        }
        return 0L;
    }

    @Override
    public List<Map<String, String>> queryParticipantList(String sectionId) throws Exception {
        if (StringUtils.isBlank(ptIdsUrl)) {
            throw new Exception("未配置平台ids地址");
        }
        BeSectionInfo beSectionInfo = beSectionInfoService.getBeSectionInfoById(sectionId);
        if(beSectionInfo == null) {
            throw new Exception("未找到标段相关信息");
        }
        Map<String, Object> params = Maps.newHashMap();
        params.put("bsID", beSectionInfo.getPtSectionId());
        params.put("busiType", "1");
        String bidViewUrl = ptIdsUrl + "/bidViewWz.shtml";
        //调用接口
        String response = RemoteService.getRemoteInfo(bidViewUrl, params);
        if (StringUtils.isNotBlank(response)) {
            Map<String, Object> resultMap = GsonUtil.jsonToMapWithGson(response);
            if (resultMap != null) {
                List<Map<String, String>> list = (List<Map<String, String>>) resultMap.get("bids");
                if (list != null) {
                    List<Map<String, String>> tempList = new ArrayList<Map<String,String>>();
                    //daizf 修改 不能遍历list时候删除list的值
                    for (Map<String, String> map : list) {
                        if (map != null && map.get("fileID") != null) {
                            tempList.add(map);
                        }
                    }
                    return tempList;
                }
            }
        }
        return null;
    }

    /**
     * 获取投标单位信息
     *
     * @param sectionId
     * @param orgCode
     * @return
     * @throws Exception
     */
    @Override
    public BeSectionParticipant getParticipantInfo(String sectionId, String orgCode) throws Exception {
        //获取第二个数据源
        
        return beSectionParticipantMapper.getParticipantInfo(sectionId, orgCode);
    }

    /**
     * 获取投标单位列表
     *
     * @param sectionId       标段Id
     * @param participantName 投标单位名称
     * @param page            分页参数
     * @param rp              分页参数
     * @return
     * @throws Exception
     */
    @Override
    public Paging<List<BeSectionParticipant>> getParticipantList(String sectionId, String participantName, int page, int rp) throws Exception {
        //获取第二个数据源
        
        //获取分页参数
        Page<BeSectionParticipant> beSectionDecryptPage = PageHelper.startPage(page, rp);
        if(StringUtils.isNotBlank(participantName)){
            participantName = "%" + participantName + "%";
        } else {
            participantName = null;
        }
        List<BeSectionParticipant> list = beSectionParticipantMapper.getParticipantList(sectionId, participantName);
        return new Paging(beSectionDecryptPage.getTotal(), list);
    }

    @Override
    public List<BeSectionParticipant> getParticipantList(String sectionId, String participantName) throws Exception {
        //获取第二个数据源
        
        return beSectionParticipantMapper.getParticipantList(sectionId, participantName);
    }

    /**
     * 展示报价
     *
     * @param sectionId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean showPrice(String sectionId) throws Exception {
        //获取第二个数据源
        
        if (!isShowPrice(sectionId)) {
            return beSectionParticipantMapper.addShowPriceInfo(sectionId) > 0;
        }
        return true;
    }

    /**
     * 是否展示报价
     *
     * @param sectionId
     * @return
     * @throws Exception
     */
    @Override
    public Boolean isShowPrice(String sectionId) throws Exception {
        //获取第二个数据源
        
        if (beSectionParticipantMapper.getShowPriceCount(sectionId) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取中标候选单位列表
     *
     * @param sectionId       标段Id
     * @return
     * @throws Exception
     */
    @Override
    public List<BeSectionParticipant> getCandidateList(String sectionId) throws Exception {
        //获取第二个数据源
        
        return beSectionParticipantMapper.getCandidateInfo(sectionId);
    }

}
